<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const arr = [1, 2, 3, 4];
        // 求和
        Array.prototype.sum = function () {
            let sum = 0;
            for (let i = 0; i < this.length; i++) {
                sum += this[i];
            }
            return sum;
        }
        console.log(arr.sum());

        // 求平方
        Array.prototype.myMap = function (callback) {
            const result = [];
            for (const item of this) {
                const newItem = callback(item);
                result.push(newItem);
            }
            return result;
        };
        const res = arr.myMap( item => item + 1);
        console.log(res);
        
        // 数组去重
        const arr1 = [1, 1, 2, 2, 3, 4, 5];
        Array.prototype.unique = function () {
            const result = [];
            for (const item of this) {
                if (!result.includes(item)) {
                    result.push(item);
                }
            }
            return result;
        }
        console.log(arr1.unique());
    </script>
</body>
</html>